﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RepositoryPattern.Infrastructure
{
    public class DALContext : IDALContext
    {
        private AppDataContext dbContext;
        private ICategoryRepository categories;
        private IProductRepository products;

        public DALContext()
        {
            dbContext = new AppDataContext();
        }
        public DALContext(string configName)
        {
            dbContext = new AppDataContext(configName);
        }
        public IProductRepository ProductRepository
        {
            get
            {
                if (products == null)
                {
                    products = new ProductRepostiroy();
                }
                return products;
            }
        }

        public ICategoryRepository CategoryRepostitory
        {
            get
            {
                if (categories == null)
                {
                    return new CategoryRepository();
                }

                return categories;
            }
        }

        public int SaveChanges()
        {
            return dbContext.SaveChanges();
        }

        public void Dispose()
        {
            if(dbContext!=null)
            {
                dbContext.Dispose();
            }
        }
    }
}